{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Split a String in Balanced Strings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: balancedStringSplit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #分割平衡字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p><strong>平衡字符串</strong> 中，<code>'L'</code> 和 <code>'R'</code> 字符的数量是相同的。</p>\n",
    "\n",
    "<p>给你一个平衡字符串&nbsp;<code>s</code>，请你将它分割成尽可能多的子字符串，并满足：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>每个子字符串都是平衡字符串。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回可以通过分割得到的平衡字符串的 <strong>最大数量</strong> <strong>。</strong></p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"RLRRLLRLRL\"\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>s 可以分割为 \"RL\"、\"RRLL\"、\"RL\"、\"RL\" ，每个子字符串中都包含相同数量的 'L' 和 'R' 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"RLRRRLLRLL\"\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>s 可以分割为 \"RL\"、\"RRRLLRLL\"，每个子字符串中都包含相同数量的 'L' 和 'R' 。\n",
    "注意，s 无法分割为 \"RL\"、\"RR\"、\"RL\"、\"LR\"、\"LL\" 因为第 2 个和第 5 个子字符串不是平衡字符串。</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"LLLLRRRR\"\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>s 只能保持原样 \"LLLLRRRR\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= s.length &lt;= 1000</code></li>\n",
    "\t<li><code>s[i] = 'L' 或 'R'</code></li>\n",
    "\t<li><code>s</code> 是一个 <strong>平衡</strong> 字符串</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [split-a-string-in-balanced-strings](https://leetcode.cn/problems/split-a-string-in-balanced-strings/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [split-a-string-in-balanced-strings](https://leetcode.cn/problems/split-a-string-in-balanced-strings/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"RLRRLLRLRL\"', '\"RLRRRLLRLL\"', '\"LLLLRRRR\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        res=0\n",
    "        now=0\n",
    "        for i in s:\n",
    "            now+=1 if i=='R' else -1\n",
    "            if not now:\n",
    "                res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        if not s:\n",
    "            return 0\n",
    "        ind = 0\n",
    "        rnum = 0\n",
    "        lnum = 0\n",
    "        while ind < len(s) and ((rnum == 0 and lnum == 0) or rnum != lnum):\n",
    "            if s[ind] == 'L':\n",
    "                lnum += 1\n",
    "            else:\n",
    "                rnum += 1\n",
    "            ind += 1\n",
    "        if rnum != lnum:\n",
    "            return 0\n",
    "        else:\n",
    "            return 1 + self.balancedStringSplit(s[ind:])\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        def split(s):\n",
    "            if not s or len(s) == 0: return\n",
    "            stack = [s[0]]\n",
    "            idx = 1\n",
    "            while len(stack) > 0 and idx < len(s):\n",
    "                if stack[-1] == s[idx]:\n",
    "                    stack.append(s[idx])\n",
    "                else:\n",
    "                    stack.pop()\n",
    "                idx += 1\n",
    "            nonlocal ans \n",
    "            ans += 1\n",
    "            if idx < len(s):\n",
    "                split(s[idx:])\n",
    "\n",
    "        \n",
    "        split(s)\n",
    "        return ans\n",
    "        \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        if not s:\n",
    "            return 0\n",
    "        rs = 0\n",
    "        for i, c in enumerate(s):\n",
    "            if c == 'R':\n",
    "                rs += 1\n",
    "            else:\n",
    "                rs -= 1\n",
    "            if rs == 0:\n",
    "                return 1 + self.balancedStringSplit(s[i+1:])\n",
    "        return rs == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        res = 0\n",
    "\n",
    "        def process(s):\n",
    "            if len(s) == 0:\n",
    "                return \n",
    "            L_num = 0\n",
    "            R_num = 0\n",
    "            i = 0\n",
    "            while True:\n",
    "                if s[i] == \"L\":\n",
    "                    L_num += 1\n",
    "                else:\n",
    "                    R_num += 1\n",
    "                if L_num == R_num and L_num != 0:\n",
    "                    nonlocal res\n",
    "                    res += 1\n",
    "                    process(s[i+1:])\n",
    "                    break\n",
    "                i += 1\n",
    "        process(s)\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ss = list(tuple(s))\n",
    "        fc, l = ss[0], len(ss)\n",
    "        ms, i, j = [], 0, 1\n",
    "        while j <= l:\n",
    "            child_ss = ss[i: j]\n",
    "            print(i, j, child_ss)\n",
    "            if child_ss.count('L') == child_ss.count('R'):\n",
    "                ms.append(child_ss)\n",
    "                i = j\n",
    "                j = i + 1\n",
    "            else:\n",
    "                j += 1\n",
    "        return len(ms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        if len(s)==0:\n",
    "            return 0\n",
    "        if len(s)==2:\n",
    "            return 1\n",
    "        for i in range(2,len(s)):\n",
    "            if s[:i].count('R')==s[:i].count('L'):\n",
    "                return 1+self.balancedStringSplit(s[i:])\n",
    "        return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        l, r = 0, 0\n",
    "        x = ''\n",
    "        ans = 0\n",
    "        for i in s:\n",
    "            x += i\n",
    "            if i == 'L':\n",
    "                l += 1\n",
    "            else:\n",
    "                r += 1\n",
    "            if x and l==r:\n",
    "                x = ''\n",
    "                ans += 1\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        count = 0\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            j = i + 1\n",
    "            if s[i] == s[j]:\n",
    "                child_s = 2\n",
    "                while child_s:\n",
    "                    j += 1\n",
    "                    if s[i] != s[j]:\n",
    "                        child_s -= 1\n",
    "                    else:\n",
    "                        child_s += 1\n",
    "            count += 1\n",
    "            i = j + 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        if n == 0:\n",
    "            return 0\n",
    "        L = 0\n",
    "        R = 0\n",
    "        for i in range(n):\n",
    "            if s[i] == \"L\":\n",
    "                L += 1\n",
    "            if s[i] == \"R\":\n",
    "                R += 1\n",
    "            if L == R:\n",
    "                return 1 + self.balancedStringSplit(s[i+1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        def dfs(s,count):\n",
    "            if not s:\n",
    "                return count\n",
    "            l=r=0\n",
    "            for i in s:\n",
    "                if i=='R':\n",
    "                    r+=1\n",
    "                if i=='L':\n",
    "                    l+=1\n",
    "                if l==r:\n",
    "                    return dfs(s[r+l:],count+1)\n",
    "        return dfs(s,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans = cur = 0\n",
    "        for c in s:\n",
    "            if c == 'L':\n",
    "                cur += 1\n",
    "            else:\n",
    "                cur -= 1\n",
    "            if not cur:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    result = 1\n",
    "\n",
    "    def balancedStringSplit(self, s):\n",
    "        for i in range(1, len(s)):\n",
    "            strL = s[0:i]\n",
    "            strR = s[i:]\n",
    "            if self.judge(strL) and self.judge(strR):\n",
    "                self.result+=1\n",
    "                self.balancedStringSplit(strL)\n",
    "                self.balancedStringSplit(strR)\n",
    "                break\n",
    "        return self.result\n",
    "\n",
    "    def judge(self, s):\n",
    "        numL = 0\n",
    "        numR = 0\n",
    "        for c in s:\n",
    "            if c == 'L':\n",
    "                numL+=1\n",
    "            else:\n",
    "                numR+=1\n",
    "        if numL == numR:\n",
    "            return True\n",
    "        else:\n",
    "            return False  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        num = 0\n",
    "        def ifbalance(s):\n",
    "            nonlocal num\n",
    "            if not s:\n",
    "                return True\n",
    "            print(s)\n",
    "            r = 0\n",
    "            l = 0\n",
    "            for ss in s:\n",
    "                if ss == \"R\":\n",
    "                    r += 1\n",
    "                if ss == \"L\":\n",
    "                    l += 1\n",
    "                if r == l:\n",
    "                    num += 1\n",
    "                    if ifbalance(s[r+l:]):\n",
    "                        return True\n",
    "        ifbalance(s)\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str,n=0,a = [0,0]) -> int:\n",
    "        if len(s)<=2:\n",
    "            return n+1\n",
    "        \n",
    "        b = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == \"L\":\n",
    "                a[0]+=1\n",
    "            else:\n",
    "                a[1]+=1\n",
    "            if a[0] == a[1]:\n",
    "                b = i+1\n",
    "                n += 1\n",
    "                if b == len(s):\n",
    "                    return n\n",
    "                break\n",
    "        s = s[b:]\n",
    "        n = self.balancedStringSplit(s,n,a)\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        l = r = 0\n",
    "        for i in range(len(s) - 2):\n",
    "            if s[i] == \"L\": l += 1\n",
    "            else: r += 1\n",
    "            if l == r: break\n",
    "        else:\n",
    "            return 1\n",
    "        return self.balancedStringSplit(s[:i+1]) + self.balancedStringSplit(s[i+1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        self.num_str = 0\n",
    "        def cut_str(s):\n",
    "            if not s:\n",
    "                return\n",
    "            n = len(s)\n",
    "            cnt_r,cnt_l = 0, 0\n",
    "            i = 0\n",
    "            while cnt_r != cnt_l or (cnt_r == 0 and cnt_l == 0):\n",
    "                \n",
    "                if s[i] == 'L':\n",
    "                    cnt_l += 1\n",
    "                else:\n",
    "                    cnt_r += 1\n",
    "                i += 1\n",
    "            self.num_str += 1\n",
    "            #print(s[i:])\n",
    "            cut_str(s[i:])\n",
    "        cut_str(s)\n",
    "        return self.num_str\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        res = 0\n",
    "        for i in s:\n",
    "            if i == 'L':\n",
    "                ans += 1 \n",
    "            else:\n",
    "                ans -=1\n",
    "            if ans == 0:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        l = len(s)\n",
    "        count = 0\n",
    "        ret = 0\n",
    "        for i in range(0,l):\n",
    "            char = s[i]\n",
    "            if char == 'L':\n",
    "                count += 1\n",
    "            elif char == 'R':\n",
    "                count -= 1\n",
    "            if count == 0:\n",
    "                ret += 1\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans, d = 0, 0\n",
    "        for ch in s:\n",
    "            if ch == 'L':\n",
    "                d += 1\n",
    "            else:\n",
    "                d -= 1\n",
    "            if d == 0:\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        L = 0\n",
    "        R = 0\n",
    "        count = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == 'L':\n",
    "                L += 1\n",
    "            elif s[i] == 'R':\n",
    "                R += 1\n",
    "            if L == R:\n",
    "                count += 1\n",
    "                L = 0\n",
    "                R = 0\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        if len( s) == 0:\n",
    "            return 0\n",
    "        else:\n",
    "            a = 0\n",
    "            b = 0\n",
    "            for j in range( len( s)):\n",
    "                if s[j] == \"R\" :\n",
    "                    a += 1\n",
    "                else:\n",
    "                    b += 1 \n",
    "                if ( a==b) and ( a>0):\n",
    "                    break \n",
    "            return 1 + self.balancedStringSplit( s[j+1:]) \n",
    "                    \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        i, k, count = 0, 2, 0\n",
    "        res_list = []\n",
    "        while i < len(s):\n",
    "            if s[i:i+k].count(\"L\") == s[i:i+k].count(\"R\"):\n",
    "                count += 1\n",
    "                i = i + k\n",
    "                k = 2\n",
    "            else:\n",
    "                k += 2\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans = cur = 0\n",
    "        for c in s:\n",
    "            if c == 'L':\n",
    "                cur += 1\n",
    "            else:\n",
    "                cur -= 1\n",
    "            if not cur:\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        l, r, res = 0, 0, 0\n",
    "        for char in s:\n",
    "            if l == r == 0:\n",
    "                if char == 'L':\n",
    "                    l += 1\n",
    "                else:\n",
    "                    r += 1\n",
    "            else:\n",
    "                if char == 'L':\n",
    "                    l += 1\n",
    "                else:\n",
    "                    r += 1\n",
    "                if l == r:\n",
    "                    res += 1\n",
    "                    l, r = 0, 0\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ret = 0\n",
    "        cnt_l = 0\n",
    "        cnt_r = 0\n",
    "        for item in s:\n",
    "            if item == \"L\":\n",
    "                cnt_l += 1\n",
    "            elif item == \"R\":\n",
    "                cnt_r += 1\n",
    "            \n",
    "            if cnt_l == cnt_r:\n",
    "                ret += 1\n",
    "                cnt_l == 0\n",
    "                cnt_r == 0\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        n,r,l=0,0,0\n",
    "        for i in s:\n",
    "            if i=='R':\n",
    "                r+=1\n",
    "            elif i =='L':\n",
    "                l+=1\n",
    "            if l==r:\n",
    "                n+=1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        count = 0\n",
    "        i = 0\n",
    "        j = 1\n",
    "        while j<=len(s)-1:\n",
    "            s1 = s[i:j]\n",
    "            if s1.count('R')==s1.count('L'):\n",
    "                count = count+1\n",
    "                i = j\n",
    "                j = i+1\n",
    "            else:\n",
    "                j = j+1\n",
    "        return count+1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        cnt1 = 0 \n",
    "        cnt2 = 0\n",
    "        cnt3 = 0\n",
    "        for i in s:\n",
    "            if i == 'R':\n",
    "                cnt1+=1\n",
    "            elif i == 'L':\n",
    "                cnt2+=1\n",
    "            if cnt1 == cnt2 and cnt1 != 0:\n",
    "                cnt3+=1\n",
    "         \n",
    "        return cnt3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        stack=[]\n",
    "        ans=0\n",
    "        for i in s:\n",
    "            stack.append(i)\n",
    "            if len(stack)>=2:\n",
    "                if (stack[-1]=='L' and stack[-2]=='R') or (stack[-1]=='R' and stack[-2]=='L'):\n",
    "                    del stack[-1]\n",
    "                    del stack[-1]\n",
    "            if len(stack)==0:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        rnums, lnums = 0, 0\n",
    "        res = 0\n",
    "        for x in s:\n",
    "            if x == 'L':\n",
    "                lnums += 1\n",
    "            else:\n",
    "                rnums += 1\n",
    "            if lnums == rnums:\n",
    "                lnums = rnums = 0\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        cnt1 = 0 \n",
    "        cnt2 = 0\n",
    "        cnt3 = 0\n",
    "        for i in s:\n",
    "            if i == 'R':\n",
    "                cnt1+=1\n",
    "            elif i == 'L':\n",
    "                cnt2+=1\n",
    "            if cnt1 == cnt2 and cnt1 != 0:\n",
    "                cnt3+=1\n",
    "                cnt1 = 0\n",
    "                cnt2 = 0\n",
    "            \n",
    "        return cnt3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        res = 0\n",
    "        a = 0\n",
    "        for i in s:\n",
    "            if i == 'R':\n",
    "                a += 1\n",
    "            else:\n",
    "                a -= 1\n",
    "            if a == 0:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        count,d=0,0\n",
    "        for i in s:\n",
    "            if i=='R':\n",
    "                d+=1\n",
    "            else:\n",
    "                d-=1\n",
    "            if d==0:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        count = 0\n",
    "        balance = 0\n",
    "        \n",
    "        for char in s:\n",
    "            if char == 'R':\n",
    "                balance += 1\n",
    "            else:\n",
    "                balance -= 1\n",
    "            \n",
    "            if balance == 0:\n",
    "                count += 1\n",
    "        \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        a = []\n",
    "        res = 0\n",
    "        for i in range(len(s)):\n",
    "            a.append(s[i])\n",
    "            if a.count(\"L\") == a.count(\"R\"):\n",
    "                a.clear()\n",
    "                res += 1\n",
    "        return res\n",
    "                \n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        result = 0\n",
    "        count = 0\n",
    "        for c in s:\n",
    "            if c == 'R':\n",
    "                count += 1\n",
    "            else:\n",
    "                count -= 1\n",
    "            if count == 0:\n",
    "                result += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        start = 0\n",
    "        result = 0\n",
    "        num_R = 0\n",
    "        num_D = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == 'R':\n",
    "                num_R += 1\n",
    "            else:\n",
    "                num_D += 1\n",
    "            if num_D == num_R and num_D:\n",
    "                result += 1\n",
    "                num_R = 0\n",
    "                num_D = 0\n",
    "        return result\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        if not s:\n",
    "            return 0\n",
    "        startLetter = s[0]\n",
    "        sameLetterCount = 1\n",
    "        res = 0\n",
    "        for c in s[1:]:\n",
    "            if startLetter=='':\n",
    "                startLetter=c\n",
    "                sameLetterCount=1\n",
    "            elif c!=startLetter:\n",
    "                sameLetterCount-=1\n",
    "                if sameLetterCount==0:\n",
    "                    res+=1\n",
    "                    startLetter=''\n",
    "            else:\n",
    "                sameLetterCount+=1\n",
    "        return res\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        n,r,l=0,0,0\n",
    "        for i in s:\n",
    "            if i=='R':\n",
    "                r+=1\n",
    "            elif i =='L':\n",
    "                l+=1\n",
    "            if l==r:\n",
    "                n+=1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        tmp=\"\"\n",
    "        count=0\n",
    "        for i in s:\n",
    "            if len(tmp.replace(\"L\",\"\"))==len(tmp.replace(\"R\",\"\")) and len(tmp.replace(\"R\",\"\"))!=0:\n",
    "                count=count+1\n",
    "                tmp= i\n",
    "            else:\n",
    "                tmp= tmp+i\n",
    "                \n",
    "        return count+1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        cnt = 0\n",
    "        res = 0\n",
    "        for i in s:\n",
    "            if i == \"L\":\n",
    "                cnt -= 1\n",
    "            else:\n",
    "                cnt += 1\n",
    "            if cnt == 0:\n",
    "                res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans, d = 0, 0\n",
    "        for ch in s:\n",
    "            if ch == 'L':\n",
    "                d += 1\n",
    "            else:\n",
    "                d -= 1\n",
    "            if d == 0:\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s):    \n",
    "        \"\"\"\n",
    "        将字符串分割为尽可能多的平衡子字符串\n",
    "        Args:\n",
    "            s (str): 原始字符串\n",
    "        returns (int): 平衡字符串的个数\n",
    "        \"\"\"\n",
    "        #处理异常情况\n",
    "        if s == None or len(s) < 2:\n",
    "            return 0 \n",
    "\n",
    "        n = len(s)\n",
    "        balance = 0 #当前子字符串的平衡系数 \n",
    "        ans = 0 \n",
    "\n",
    "        #从头开始遍历字符串, 遇到L字符串 + 1, R字符串-1, 当正好balance == 0时, 即可将其划分, ans += 1\n",
    "        for i in range(n):\n",
    "            if s[i] == 'L':\n",
    "                balance += 1\n",
    "            else:\n",
    "                balance -= 1\n",
    "\n",
    "            if balance == 0:\n",
    "                ans += 1\n",
    "        \n",
    "        #return \n",
    "        return ans \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        num = 0\n",
    "        length = 0\n",
    "        for index in range(len(s)):\n",
    "            if s[index] == 'L':\n",
    "                num += 1\n",
    "            else:\n",
    "                num -= 1\n",
    "            \n",
    "            if num == 0:\n",
    "                length += 1\n",
    "\n",
    "        return length"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        balance=0\n",
    "        result=0\n",
    "        for i in range(len(s)):\n",
    "            if s[i]=='L':\n",
    "                balance+=1\n",
    "            else:\n",
    "                balance-=1\n",
    "            if balance==0:\n",
    "                result+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans, cnt = 0, 0\n",
    "        for c in s:\n",
    "            if c == 'L':\n",
    "                cnt += 1\n",
    "            else:\n",
    "                cnt -= 1\n",
    "            if cnt == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        d=0\n",
    "        ans=0\n",
    "        for c in s:\n",
    "            if c == 'L':\n",
    "                d+=1\n",
    "            else:\n",
    "                d-=1\n",
    "            if d==0:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans,d=0,0\n",
    "        for ch in s:\n",
    "            if ch=='L':\n",
    "                d+=1\n",
    "            else:\n",
    "                d-=1\n",
    "            if d==0:\n",
    "                ans+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        freqL, freqR = 0 ,0\n",
    "        split = 0\n",
    "        for alpha in s:\n",
    "            if alpha=='L':\n",
    "                freqL+=1\n",
    "            else:\n",
    "                freqR+=1\n",
    "            if freqL==freqR and freqL!=0:\n",
    "                freqL=0\n",
    "                freqR=0\n",
    "                split+=1\n",
    "        return split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        balance = 0\n",
    "        for ch in s:\n",
    "            if ch == 'R':\n",
    "                balance += 1\n",
    "            else:\n",
    "                balance -= 1\n",
    "            if balance == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        i,j,m=0,0,0\n",
    "        for k in range(len(s)):\n",
    "            if s[k]=='L':\n",
    "                i+=1\n",
    "            else:\n",
    "                j+=1\n",
    "            if i == j:\n",
    "                m+=1\n",
    "                i,j=0,0\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        a=b=c=0\n",
    "        for i in range(len(s)):\n",
    "            if s[i]=='R':\n",
    "                a=a+1\n",
    "            else:\n",
    "                b=b+1\n",
    "            if a==b:\n",
    "                c=c+1\n",
    "                a=0\n",
    "                b=0\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        stack = []\n",
    "        counts = 0\n",
    "        for char in s:\n",
    "            if len(stack) == 0:\n",
    "                stack.append(char)\n",
    "            else:\n",
    "                if char == 'R':\n",
    "                    if stack[len(stack)-1] == 'L':\n",
    "                        stack.pop()\n",
    "                        if len(stack) == 0:\n",
    "                            counts += 1\n",
    "                    else:\n",
    "                        stack.append(char)\n",
    "                elif char == 'L':\n",
    "                    if stack[len(stack)-1] == 'R':\n",
    "                        stack.pop()\n",
    "                        if len(stack) == 0:\n",
    "                            counts += 1\n",
    "                    else:\n",
    "                        stack.append(char)\n",
    "            # print(stack)\n",
    "        return counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        res=0\n",
    "        R,L=0,0\n",
    "        for c in s:\n",
    "            if c=='R':\n",
    "                R+=1\n",
    "            else:\n",
    "                L+=1\n",
    "            if R==L:\n",
    "                res+=1\n",
    "                R,L=0,0\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans = a = 0\n",
    "        for x in s:\n",
    "            if x == \"L\":\n",
    "                a += 1\n",
    "            else:\n",
    "                a -= 1\n",
    "            if not a:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        count, res = 0, 0\n",
    "\n",
    "        for ch in s:\n",
    "            if ch == 'R':\n",
    "                count += 1 \n",
    "            else:\n",
    "                count -= 1\n",
    "            if count == 0:\n",
    "                res += 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        l, r = 0, 0\n",
    "        ans = 0\n",
    "        for c in s:\n",
    "            if c == 'L':\n",
    "                l += 1\n",
    "            else:\n",
    "                r += 1\n",
    "            if l == r:\n",
    "                ans += 1\n",
    "                l, r = 0, 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        n,r,l=0,0,0\n",
    "        for i in s:\n",
    "            if i=='R':\n",
    "                r+=1\n",
    "            elif i =='L':\n",
    "                l+=1\n",
    "            if l==r:\n",
    "                n+=1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        return sum(s[:i+2].count('L') == s[:i+2].count('R') for i in range(0, len(s), 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans, d = 0, 0\n",
    "        for ch in s:\n",
    "            if ch == 'L':\n",
    "                d += 1\n",
    "            else:\n",
    "                d -= 1\n",
    "            if d == 0:\n",
    "                ans += 1\n",
    "        return ans\n",
    "\n",
    "# 作者：力扣官方题解\n",
    "# 链接：https://leetcode.cn/problems/split-a-string-in-balanced-strings/solutions/963965/fen-ge-ping-heng-zi-fu-chuan-by-leetcode-7y8u/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        stack = []\n",
    "        for c in s:\n",
    "            if stack == []:\n",
    "                stack.append(c)\n",
    "            elif stack[-1] == c:\n",
    "                stack.append(c)\n",
    "            else:\n",
    "                stack.pop()\n",
    "            if stack == []:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans, d = 0, 0\n",
    "        for ch in s:\n",
    "            if ch == 'L':\n",
    "                d += 1\n",
    "            else:\n",
    "                d -= 1\n",
    "            if d == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        qzq=0\n",
    "        ans=0\n",
    "        for i in list(s):\n",
    "            if i==\"R\":\n",
    "                qzq=qzq+1\n",
    "                if qzq==0:\n",
    "                    ans=ans+1\n",
    "            else:\n",
    "                qzq=qzq-1\n",
    "                if qzq==0:\n",
    "                    ans=ans+1\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        k = 0\n",
    "        count = 0\n",
    "        for i in range(2, len(s), 2):\n",
    "            if s[k:i].count(\"L\") == s[k:i].count(\"R\"):\n",
    "                 count+=1\n",
    "                 k = i\n",
    "        return count + 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        res = 0\n",
    "\n",
    "        p = 0\n",
    "        tag = 0\n",
    "        n = len(s)\n",
    "        for c in s:\n",
    "            if c is 'L':\n",
    "                tag += 1\n",
    "            else:\n",
    "                tag -= 1\n",
    "            if tag == 0:\n",
    "                res += 1\n",
    "            p += 1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans, d = 0, 0\n",
    "        for ch in s:\n",
    "            if ch == 'L':\n",
    "                d += 1\n",
    "            else:\n",
    "                d -= 1\n",
    "            if d == 0:\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def balancedStringSplit(self, s: str) -> int:\n",
    "#         l , r , ans = 0 , 0 , 0\n",
    "#         for i in s:\n",
    "#             if i == 'R':\n",
    "#                 r += 1\n",
    "#             elif i == 'L':\n",
    "#                 l += 1\n",
    "#             if l == r:\n",
    "#                 ans +=1\n",
    "#                 l , r = 0 , 0\n",
    "#         return ans\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        res = 0\n",
    "        state = 0\n",
    "        for i in s:\n",
    "            if i == 'R':\n",
    "                state+=1\n",
    "            else:\n",
    "                state-=1\n",
    "            if state==0:\n",
    "                res +=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        L = R = ans = 0\n",
    "        for ch in s:\n",
    "            if ch == 'L':\n",
    "                L += 1\n",
    "            else:\n",
    "                R += 1\n",
    "            if L > 0 and L == R:\n",
    "                ans += 1\n",
    "                L = R = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        c=ans=0\n",
    "        for i in s:\n",
    "            if i=='R':\n",
    "                ans-=1\n",
    "            else:\n",
    "                ans+=1\n",
    "            if ans==0:\n",
    "                c+=1\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        list = []\n",
    "        num = 0\n",
    "        length = 0\n",
    "        for index in range(len(s)):\n",
    "            length = length + 1\n",
    "            if s[index] == 'L':\n",
    "                num = num + 1\n",
    "            else:\n",
    "                num = num - 1\n",
    "            \n",
    "            if num == 0:\n",
    "                list.append(s[index - length + 1 : index + 1])\n",
    "                length = 0\n",
    "\n",
    "        return len(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        count = 0\n",
    "        for string in s:\n",
    "            \n",
    "            if string == \"L\":\n",
    "                count += 1\n",
    "            else:\n",
    "                count -= 1\n",
    "            if count == 0:\n",
    "                ans += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def balancedStringSplit(self, s: str) -> int:\n",
    "        ans, d = 0, 0\n",
    "        for ch in s:\n",
    "            if ch == 'L':\n",
    "                d += 1\n",
    "            else:\n",
    "                d -= 1\n",
    "            if d == 0:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
